//
//  SDPromotionsTableViewController.m
//  Sydeny
//
//  Created by Rainbow on 7/23/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SDPromotionsTableViewController.h"
#import "SDTwoAdvertisingView.h"
#import "CommonShopsTableViewController.h"
#import "SearchShopsTableViewController.h"
#import "ServiceHelper.h"

@implementation SDPromotionsTableViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    self.listType = SydenyShopListTypePromotions;
    [super viewDidLoad];
    
    UIImageView *logoView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo.png"]] autorelease];
    logoView.frame = CGRectMake(0, 0, 94, 29);
    self.navigationItem.titleView = logoView;
    
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 164)] autorelease];
    
    
    SDTwoAdvertisingView *adView = [[SDTwoAdvertisingView alloc] initWithAds:[[CacheData sharedData].advertisings objectForKey:@"1"]];
    adView.delegate = self;
    [headerView addSubview:adView];
    [adView release];
    
//    [self.view addSubview:recommandBgView];
//    recommandBgView.frame = CGRectMake(0, 80, 300, 176);
//    [headerView addSubview:recommandBgView];
//    
//    recommendTableView.delegate = self;
//    recommendTableView.dataSource = self;
    
    self.tableView.tableHeaderView = headerView;
    
    [requestParams setObject:@"1" forKey:@"page"];
    [requestParams setObject:[NSString stringWithFormat:@"%d",PER_PAGE_NUMBER] forKey:@"limit"];
    [requestParams setObject:@"1" forKey:@"type"];
    
    [[ServiceHelper sharedService] requestServerForMethod:SydenyRequestGetShops WithParameters:requestParams AndCallBackScope:self AndSuccessSel:@"getShopsSuccess:" AndFailedSel:@"getShopsFailed:"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshData) name:NN_ENTER_FOREGROUND object:nil];
    
}

- (void)refreshData
{
    [[ServiceHelper sharedService] requestServerForMethod:SydenyRequestGetShops WithParameters:requestParams AndCallBackScope:self AndSuccessSel:@"getShopsSuccess:" AndFailedSel:@"getShopsFailed:"];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (NSInteger)tableView:(UITableView *)_tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    if (_tableView.tag == 100) {
        return 4;
    }
    else
        return [super tableView:_tableView numberOfRowsInSection:section];
}

- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView.tag == 100) {
        
        UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
        
        UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"most_%d.png", indexPath.row + 1]]] autorelease];
        imageView.frame = CGRectMake(20, 14, 15, 15);
        [cell.contentView addSubview:imageView];
        
        NSString *mostStr = @"";
        switch (indexPath.row) {
            case 0:
                mostStr = @"本周热门";
                break;
            case 1:
                mostStr = @"收藏最多";
                break;
            case 2:
                mostStr = @"附近推荐";
                break;
            case 3:
                mostStr = @"店铺搜索";
                break;
            default:
                break;
        }
        
        UILabel *nameLabel = [[[UILabel alloc] initWithFrame:CGRectMake(50, 12, 100, 20)] autorelease];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.text = mostStr;
        [cell.contentView addSubview:nameLabel];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    }
    else
    {
        return [super tableView:_tableView cellForRowAtIndexPath:indexPath];
    }
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)_tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView.tag == 100) {
        
        if (indexPath.row == 3) {
            SearchShopsTableViewController *searchShopsTableViewController = [[SearchShopsTableViewController alloc] initWithNibName:@"SDShopsTableViewController" bundle:nil];
            [self.navigationController pushViewController:searchShopsTableViewController animated:YES];
            [searchShopsTableViewController release];
            [_tableView deselectRowAtIndexPath:indexPath animated:YES];
            return;
        }
        
        NSMutableDictionary *params = [[[NSMutableDictionary alloc] initWithCapacity:0] autorelease];
        [params setObject:@"1" forKey:@"page"];
        [params setObject:[NSString stringWithFormat:@"%d",PER_PAGE_NUMBER] forKey:@"limit"];
        [params setObject:@"1" forKey:@"type"];
        NSString *title = @"";
        SydenyRequestType requestType;
        BOOL canGetMore = YES;
        switch (indexPath.row) {
            case 0:
                title = @"本周热门";
                requestType = SydenyRequestGetWeekHot;
                canGetMore = NO;
                break;
                
            case 1:
                title = @"收藏最多";
                requestType = SydenyRequestGetCollectMost;
                canGetMore = NO;
                break;
                
            case 2:
                title = @"附近推荐";
                requestType = SydenyRequestGetShops;
                if ([CacheData sharedData].latitude != nil && [CacheData sharedData].latitude != nil) {
                    
                    [params setObject:[CacheData sharedData].latitude forKey:@"latitude"];
                    [params setObject:[CacheData sharedData].longitude forKey:@"longtitude"];
                }
                break;
                
            case 3:
                title = @"店铺搜索";
                requestType = SydenyRequestGetShops;
                break;
                
            default:
                break;
        }
        
        CommonShopsTableViewController *commonShopsTableViewController = [[CommonShopsTableViewController alloc] initWithRequsetParams:params withTitle:title RequestType:requestType];
        commonShopsTableViewController.loadFinish = !canGetMore;
        [self.navigationController pushViewController:commonShopsTableViewController animated:YES];
        [commonShopsTableViewController release];
        
        [_tableView deselectRowAtIndexPath:indexPath animated:YES];
    }
    else
        return [super tableView:_tableView didSelectRowAtIndexPath:indexPath];
}

- (CGFloat)tableView:(UITableView *)_tableView heightForHeaderInSection:(NSInteger)section
{
    if (_tableView.tag == 100) {
        return 0;
    }
    else
        return [super tableView:_tableView heightForHeaderInSection:section];
}

- (CGFloat)tableView:(UITableView *)_tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_tableView.tag == 100) {
        return 44;
    }
    else
        return [super tableView:_tableView heightForRowAtIndexPath:indexPath];

}

- (UIView *)tableView:(UITableView *)_tableView viewForHeaderInSection:(NSInteger)section
{
    if (_tableView.tag == 100) {
        return nil;
    }
    else
    {
        UIView *bgView = [super tableView:_tableView viewForHeaderInSection:section];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 20)];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = [UIColor whiteColor];
        label.font = [UIFont systemFontOfSize:14];
        label.text = @"好康优惠";
        [bgView addSubview:label];
        [label release];
        return bgView;
    }
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
